package com.healthy.common;

import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Res getException(Exception ex) {
        return new Res(0, ex.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Res formCheckException(MethodArgumentNotValidException ex) {
        String result = "校验失败：";
        for (FieldError error : ex.getFieldErrors()) {
            result += error.getDefaultMessage() + "；";
        }
        return new Res(0, result);
    }
}
